package cn.dakaqi.services.impl;

import cn.dakaqi.dao.JobDao;
import cn.dakaqi.entities.Job;
import cn.dakaqi.services.JobService;
import cn.dakaqi.utils.DKQConstant;
import cn.dakaqi.utils.exception.ServiceRuntimeException;
import cn.dakaqi.utils.redis.RedisClientTemplate;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chunyang on 2016/7/12.
 */
@Component(value = "jobService")
@Transactional
public class JobServiceImpl implements JobService
{
    @Autowired
    RedisClientTemplate redisClientTemplate;
    @Autowired
    JobDao jobDao;
    @Autowired
    RedisTemplate redisTemplate;
    private String key = "Job";
    @Override
    public void save(Job job) throws ServiceRuntimeException
    {
//        Key类型操作
//        ValueOperations 	Redis String/Value 操作
//        ListOperations 	Redis List 操作
//        SetOperations 	Redis Set 操作
//        ZSetOperations 	Redis Sort Set 操作
//        HashOperations 	Redis Hash 操作
//        Value约束操作
//        BoundValueOperations 	Redis String/Value key 约束
//        BoundListOperations 	Redis List key 约束
//        BoundSetOperations 	Redis Set key 约束
//        BoundZSetOperations 	Redis Sort Set key 约束
//        BoundHashOperations 	Redis Hash key 约束
//        redisTemplate.execute(new RedisCallback<Job>()
//        {
//            @Override
//            public Job doInRedis(RedisConnection redisConnection) throws DataAccessException
//            {
//                return null;
//            }
//        });

        jobDao.save(job);

        HashOperations hashOper = redisTemplate.opsForHash();
        HashMap map=new LinkedHashMap();
        map.put(job.getId(),job);
        hashOper.putAll(key,map);
    }

    @Override
    public List<Job> queryAllByClient(String client) throws ServiceRuntimeException
    {
        key = key + "_"+client;
        List<Job> list = null;
//        ListOperations listOper = redisTemplate.opsForList();
//        list = (List<Job>)listOper.leftPop(key);
//        if(null != list && list.size()>0)
//        {
//            return list;
//        }
//        else
//        {
//            Map<String, SearchFilter> filters = Maps.newHashMap();
//            filters.put("client", new SearchFilter("client", SearchFilter.Operator.EQ, client));
//            filters.put("delStatus", new SearchFilter("delStatus", SearchFilter.Operator.EQ, DKQConstant.DEL_NO));
//            Specification<Job> spec = DynamicSpecifications.bySearchFilter(filters.values(), Job.class);
//            list = this.jobDao.findAll(spec);
//
//            listOper.rightPushAll(key,list);
//        }

        Sort sort = new Sort(Sort.Direction.ASC,"code");
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("client", new SearchFilter("client", SearchFilter.Operator.EQ, client));
        filters.put("delStatus", new SearchFilter("delStatus", SearchFilter.Operator.EQ, DKQConstant.DEL_NO));
        Specification<Job> spec = DynamicSpecifications.bySearchFilter(filters.values(), Job.class);
        list = this.jobDao.findAll(spec,sort);
        return list;
    }

    @Override
    public void delete(Long id) throws ServiceRuntimeException
    {
        Job job = findOne(id);
        if(job != null)
        {
            job.setDelStatus(DKQConstant.DEL_YES);
            this.save(job);
        }
    }

    @Override
    public Job findOne(long id) throws ServiceRuntimeException
    {
        return jobDao.findOne(id);
    }
}
